package com.mf.ergate.web.controller.auth;

import com.mf.ergate.api.auth.model.dto.ResourceInfoCreateDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoDeleteDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoQueryDto;
import com.mf.ergate.api.auth.model.dto.ResourceInfoUpdateDto;
import com.mf.ergate.api.auth.model.vo.ResourceInfoVo;
import com.mf.ergate.web.common.exception.BusinessException;
import com.mf.ergate.web.common.utils.MfParamUtils;
import com.mf.ergate.web.domain.enums.auth.ResourceTypeEnum;
import com.mf.ergate.web.domain.enums.generic.AppTypeEnum;
import com.mf.ergate.web.service.auth.ResourceInfoService;
import com.mf.framework.common.model.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 应用资源信息Controller
 * User: zhaoming
 * DateTime: 2018-03-02
 * To change this template use File | Settings | File Templates.
 */
@Secured({"MANAGE_RESOURCE_INFO"})
@Controller
@RequestMapping("/web/resourceInfo")
public class ResourceInfoController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private ResourceInfoService resourceInfoService;

    /**
     * 首页
     *
     * @return 模型视图
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
        return new ModelAndView("auth/resource/resource_info_index");
    }

    /**
     * 新增页面
     *
     * @param uniqueNo 资源编号
     * @return 模型视图
     */
    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public ModelAndView addPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("auth/resource/resource_info_add");
        // 应用类型
        modelAndView.addObject("AppTypeEnum", AppTypeEnum.getList());
        // 资源类型
        modelAndView.addObject("ResourceTypeEnum", ResourceTypeEnum.getList());
        // 资源编号
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 维护页面
     *
     * @param uniqueNo 资源编号
     * @return 模型视图
     */
    @RequestMapping(value = "/editPage", method = RequestMethod.GET)
    public ModelAndView editPage(String uniqueNo) {
        ModelAndView modelAndView = new ModelAndView("auth/resource/resource_info_edit");
        // 应用类型
        modelAndView.addObject("AppTypeEnum", AppTypeEnum.getList());
        // 资源类型
        modelAndView.addObject("ResourceTypeEnum", ResourceTypeEnum.getList());
        // 资源编号
        modelAndView.addObject("uniqueNo", uniqueNo);
        return modelAndView;
    }

    /**
     * 新增
     *
     * @param resourceInfoCreateDto 资源信息新增Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ResourceInfoVo insert(ResourceInfoCreateDto resourceInfoCreateDto) {
        MfParamUtils.buildInsertEntity(resourceInfoCreateDto);
        return resourceInfoService.insert(resourceInfoCreateDto);
    }

    /**
     * 修改
     *
     * @param resourceInfoUpdateDto 资源信息更新Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Boolean update(ResourceInfoUpdateDto resourceInfoUpdateDto) {
        // 验证资源类型修改，下是否存在子节点
        ResourceInfoVo resourceInfoVo = resourceInfoService.selectByUniqueNo(resourceInfoUpdateDto.getUniqueNo());
        if (!Objects.equals(resourceInfoUpdateDto.getResourceTypeCode(), resourceInfoVo.getResourceTypeCode())) {
            ResourceInfoQueryDto resourceInfoQueryDto = new ResourceInfoQueryDto();
            resourceInfoQueryDto.setParentResourceCode(resourceInfoUpdateDto.getResourceCode());
            if (resourceInfoService.selectCount(resourceInfoQueryDto) > 0) {
                throw new BusinessException("此资源下存在子节点，不能将资源类型改为[节点]");
            }
        }
        // 设置更新信息
        MfParamUtils.buildUpdateEntity(resourceInfoUpdateDto);
        return resourceInfoService.updateByUniqueNo(resourceInfoUpdateDto);
    }

    /**
     * 根据主键删除资源相关信息
     *
     * @param resourceInfoDeleteDto 资源信息删除Dto
     * @return 执行结果
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public boolean delete(ResourceInfoDeleteDto resourceInfoDeleteDto) {
        MfParamUtils.buildUpdateEntity(resourceInfoDeleteDto);
        return resourceInfoService.deleteCompleteByUniqueNo(resourceInfoDeleteDto);
    }

    /**
     * 根据主键查询
     *
     * @param resourceInfoQueryDto 资源信息查询Dto
     * @return 应用资源信息
     */
    @RequestMapping(value = "/detail", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public ResourceInfoVo detail(ResourceInfoQueryDto resourceInfoQueryDto) {
        return resourceInfoService.selectByUniqueNo(resourceInfoQueryDto.getUniqueNo());
    }

    /**
     * 根据条件查询
     *
     * @param resourceInfoQueryDto 资源信息查询Dto
     * @return 应用资源信息列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public List<ResourceInfoVo> list(ResourceInfoQueryDto resourceInfoQueryDto) {
        return resourceInfoService.selectByCondition(resourceInfoQueryDto);
    }

    /**
     * 根据条件分页查询
     *
     * @param resourceInfoQueryDto 资源信息查询Dto
     * @param pageNum      当前页码
     * @param pageSize     每页显示条数
     * @param sortName     排序字段
     * @param sortOrder    排序方式
     * @return 应用资源信息分页列表
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Page<ResourceInfoVo> page(ResourceInfoQueryDto resourceInfoQueryDto,
                                     @RequestParam(value = "page") int pageNum,
                                     @RequestParam(value = "rows") int pageSize,
                                     @RequestParam(value = "sidx") String sortName,
                                     @RequestParam(value = "sord") String sortOrder) {

        Page page = MfParamUtils.buildPage(pageNum, pageSize, sortName, sortOrder);
        return resourceInfoService.selectPage(page, resourceInfoQueryDto);
    }

    /**
     * 检查重复数据
     *
     * @return 验证结果
     */
    @RequestMapping(value = "/checkRepeat", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> checkRepeat(ResourceInfoQueryDto resourceInfoQueryDto) {
        Map<String, Object> map = new HashMap<>();
        if (resourceInfoService.selectCount(resourceInfoQueryDto) > 0) {
            map.put("error", "资源编码已被占用");
        }
        return map;
    }

}